package com.example.yichuangyun.mvvm.ui.adapter;

import static android.app.Activity.RESULT_OK;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemSearchNearbyCompanyBinding;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.SearchHistoryUtil;
import com.example.yichuangyun.utils.TextUtilTools;

public class SearchNearbyCompanyAdapter extends BaseQuickAdapter<CompanyDataModel.CompanyModel, DataBindingHolder<ItemSearchNearbyCompanyBinding>> {

    private final Context context;
    private String keywords;

    public SearchNearbyCompanyAdapter(Context context) {
        this.context = context;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemSearchNearbyCompanyBinding> nearbyCompanyBindingDataBindingHolder, int i, @Nullable CompanyDataModel.CompanyModel model) {
        ItemSearchNearbyCompanyBinding binding = nearbyCompanyBindingDataBindingHolder.getBinding();
        if (null != model) {
            Glide.with(context).load(model.getLogoUrl()).placeholder(R.mipmap.icon_company_def_logo).into(binding.ivCompanyLogo);
            if (!TextUtils.isEmpty(keywords) && !TextUtils.isEmpty(model.getName())) {
                SpannableStringBuilder spannableStringBuilder = TextUtilTools.highlight(model.getName(), keywords);
                binding.tvCompanyName.setText(spannableStringBuilder);
            } else {
                binding.tvCompanyName.setText(CommonUtil.setText2(model.getName()));
            }
            binding.tvAddress.setText(CommonUtil.setText3(TextUtils.isEmpty(model.getReportAddress()) ? model.getAddress() : model.getReportAddress()));

            binding.llItem.setOnClickListener(view -> {
                Intent intent = new Intent();
                intent.putExtra(Constant.modelKey, model);
//                Bundle bundle = new Bundle();
//                bundle.putDouble(Constant.latitudeKey, model.getLatitude());
//                bundle.putDouble(Constant.longitudeKey, model.getLongitude());
//                intent.putExtras(bundle);
                ((Activity) context).setResult(RESULT_OK, intent);
                CommonUtil.closeKeyboard((Activity) context);
                SearchHistoryUtil.saveKeyWord(keywords);
                ((Activity) context).finish();
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemSearchNearbyCompanyBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_search_nearby_company, viewGroup);
    }
}
